package com.yisu.kabao.manager.core.service.impl;

import com.yisu.kabao.ewf.common.web.EasyUIGrid;
import com.yisu.kabao.ewf.entity.Advertisement;
import com.yisu.kabao.manager.core.mapper.AdvertisementMapper;
import com.yisu.kabao.manager.core.service.IAdvertisementService;
import com.yisu.kabao.manager.domain.request.baseInfoPackage.AdvertisementRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：广告位业务接口实现类
 * createTime：2017/8/10
 */
@Service("IAdvertisementService")
public class AdvertisementServiceImpl extends BaseService implements IAdvertisementService {
    @Resource
    private AdvertisementMapper advertisementMapper;

    /**
     * author:赵杰
     * Business Description : 通过主键ID，获取广告位信息
     *
     * @param primaryId 主键信息
     * @return
     */
    @Override
    public Advertisement getAdvertisementModelByPrimaryId(long primaryId) {
        return advertisementMapper.selectByPrimaryKey(primaryId);
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，删除广告位信息
     *
     * @param primaryId 主键信息
     * @return
     */
    @Override
    public boolean deleteAdvertisementByPrimaryId(long primaryId) {
        return advertisementMapper.deleteByPrimaryKey(primaryId) > 0;
    }

    /**
     * author:赵杰
     * Business Description : 编辑广告位信息
     *
     * @param advertisement
     * @return
     */
    @Override
    public boolean modifyAdvertisement(Advertisement advertisement) {
        try {
            Advertisement checkModel = advertisementMapper.selectByPrimaryKey(advertisement.getId());
            if (checkModel == null) {
                advertisement.setCreateDate(new Date());
                advertisement.setIsValid(true);
                return advertisementMapper.insertSelective(advertisement) > 0;
            } else {
                Advertisement updateModel = new Advertisement();
                updateModel.setUpdateDate(new Date());
                updateModel.setSort(advertisement.getSort());
                updateModel.setName(advertisement.getName());
                updateModel.setProductId(advertisement.getProductId());
                updateModel.setImage(advertisement.getImage());
                updateModel.setUrl(advertisement.getUrl());
                updateModel.setAppSign(advertisement.getAppSign());
                updateModel.setId(advertisement.getId());
                return advertisementMapper.updateByPrimaryKeySelective(updateModel) > 0;
            }
        } catch (Exception e) {
            logger.error("编辑广告位信息异常：" + e.getMessage());
        }
        return false;
    }

    /**
     * author:赵杰
     * Business Description : 分页获取广告位列表
     *
     * @param advertisementRequest 分页请求信息
     * @return 广告位列表信息
     */
    @Override
    public EasyUIGrid<Advertisement> getAdvertisementByPage(AdvertisementRequest advertisementRequest) {
        List<Advertisement> advertisements = advertisementMapper.getAdvertisementByPage(advertisementRequest);
        EasyUIGrid<Advertisement> gridData = new EasyUIGrid<>();
        gridData.setTotal(advertisementRequest.getTotalRecords());
        gridData.setRows(advertisements);
        return gridData;
    }
}
